﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;

public struct S_DynamicAtlasSheet
{
    public string Name;
    public int x;
    public int y;
    public int width;
    public int height;
    public Texture2D Texture;
    public Texture2D MainTexture;
    public System.Action<S_DynamicAtlasSheet> Call;
}

/// <summary>
/// 动态合并管理器
/// </summary>
public class DynamicAtlasManager
{
    /// <summary>图集列表</summary>
    static Dictionary<string, List<TextureCombine>> DynamicIconAtlasDic = new Dictionary<string, List<TextureCombine>>();
    /// <summary>正在下载列表</summary>
    static Dictionary<string, List<S_DynamicAtlasSheet>> mInLoadingDic = new Dictionary<string, List<S_DynamicAtlasSheet>>();

    public static void SetTexture(string id,string path, System.Action<S_DynamicAtlasSheet> call,string group = "default")
    {
        List<TextureCombine> DynamicIconAtlasList;

        if(!DynamicIconAtlasDic.TryGetValue(group,out DynamicIconAtlasList))
        {
            DynamicIconAtlasList = new List<TextureCombine>();
            DynamicIconAtlasDic.Add(group, DynamicIconAtlasList);
        }

        DynamicIconAtlasList = DynamicIconAtlasDic[group];

        int cnt = DynamicIconAtlasList.Count;
        TextureCombine curCombine = null;
        for (int i = 0; i < cnt; i++)
        {
            curCombine = DynamicIconAtlasList[i];
            S_DynamicAtlasSheet spData = curCombine.GetSheetData(id);
            if (string.IsNullOrEmpty(spData.Name))
            {
                call(spData);
                return;
            }
        }

        S_DynamicAtlasSheet newSheet = default(S_DynamicAtlasSheet);
        newSheet.Name = id;
        newSheet.Call = call;

        List<S_DynamicAtlasSheet> list;
        bool haveLoading = false;
        if (mInLoadingDic.TryGetValue(id, out list))
        {
            haveLoading = true;
            list.Add(newSheet);
            mInLoadingDic[id] = list;
        }
        else
        {
            list = new List<S_DynamicAtlasSheet>();
            list.Add(newSheet);
            mInLoadingDic.Add(id, list);
        }

        if (!haveLoading)
        {
            Addressables.LoadAssetAsync<TextAsset>(path).Completed += (op) =>
            {
                S_DynamicAtlasSheet sheetData = default(S_DynamicAtlasSheet);

                lock (DynamicIconAtlasList)
                {
                    int knt = DynamicIconAtlasList.Count;
                    for (int i = 0; i < knt; i++)
                    {
                        curCombine = DynamicIconAtlasList[i];

                        sheetData = curCombine.Combine(op.Result.bytes,id);
                        if (string.IsNullOrEmpty(sheetData.Name))
                        {
                            curCombine = null;
                            continue;
                        }
                    }

                    if (curCombine == null)
                    {
                        curCombine = new TextureCombine();
                        sheetData = curCombine.Combine(op.Result.bytes,id);

                        DynamicIconAtlasList.Add(curCombine);
                    }

                    DynamicIconAtlasDic[group] = DynamicIconAtlasList;
                    lock (mInLoadingDic)
                    {
                        List<S_DynamicAtlasSheet> list2;
                        if (mInLoadingDic.TryGetValue(id, out list2))
                        {
                            int lnt = list2.Count;
                            for (int li = 0; li < lnt; li++)
                            {
                                list2[li].Call(sheetData);
                            }

                            mInLoadingDic.Remove(id);
                        }
                    }
                }
            };
        }
    }
}
